﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;

namespace PQ.Helper
{
    /// <summary>
    /// 序列化
    /// </summary>
    public class SerializeHelper {

        #region 2 进制流方式序列化

        /// <summary>
        /// 2 进制流方式序列化
        /// </summary>
        /// <param name="obj">序列化对象</param>
        /// <returns> base64 字符串</returns>
        public static string BinarySerialize(object obj) {
            using (Stream stream = new MemoryStream()) {
                BinaryFormatter formatter = new BinaryFormatter();
                // 设置类型可防止出错 binaryHeader 找不到的错误
                formatter.TypeFormat = FormatterTypeStyle.XsdString;
                formatter.Serialize(stream, obj);
                // 序列化后将流的指针位置改回
                stream.Position = 0;
                // 读取流到 byte 中
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                // 转化成 base64 字符串返回
                return Convert.ToBase64String(bytes, Base64FormattingOptions.None);
            }
        }
        /// <summary>
        /// 2 进制流方式反序列化
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="base64String">base64 字符串</param>
        /// <returns>对象</returns>
        public static T BinaryDeserialize<T>(string base64String) {
            byte[] bytes = Convert.FromBase64String(base64String);
            using (Stream stream = new MemoryStream(bytes)) {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.TypeFormat = FormatterTypeStyle.XsdString;
                stream.Position = 0;
                return (T)formatter.Deserialize(stream);
            }
        }

        #endregion

        #region XML 方式序列化

        /// <summary>
        /// XML 方式序列化
        /// </summary>
        /// <param name="obj">序列化对象</param>
        /// <returns>序列化后的 XML 字符串</returns>
        public static string XMLSerialize(object obj) {
            XmlSerializer xsFormatter = new XmlSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            XmlTextWriter xtw = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
            xtw.Formatting = Formatting.Indented;
            xsFormatter.Serialize(xtw, obj);
            stream.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(stream);
            string str = sr.ReadToEnd();
            xtw.Close();
            stream.Close();
            return str;
        }
        /// <summary>
        /// XML 方式反序列化
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="xml">XML 字符串</param>
        /// <param name="type">要生成的对象 type</param>
        /// <returns>对象</returns>
        public static T XMLDeserialize<T>(string xml, Type type) {
            XmlSerializer xs = new XmlSerializer(type);
            StringReader sr = new StringReader(xml);
            return (T)xs.Deserialize(sr);
        }

        #endregion


    }
}
